package com.j2eefast.bussiness.contract.service;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j2eefast.bussiness.contract.entity.PayPieceEntity;
import com.j2eefast.bussiness.contract.entity.PayTimingEntity;
import com.j2eefast.bussiness.contract.mapper.BusEquipmentMapper;
import com.j2eefast.bussiness.contract.mapper.PayPieceMapper;
import com.j2eefast.bussiness.contract.mapper.PayTimingMapper;
import com.j2eefast.bussiness.contract.utils.StringUtils;
import com.j2eefast.common.core.page.Query;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.framework.annotation.DataFilter;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * <p>计件工资</p>
 *
 * @author: summer
 * @date: 2021-05-31 14:41
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */

@Service
public class PayPieceService extends ServiceImpl<PayPieceMapper, PayPieceEntity> {

    @Resource
    protected PayPieceMapper payPieceMapper;

    @Resource
    protected BusEquipmentMapper busEquipmentMapper;

    /**
     * 页面分页查询
     */
    public PageUtil findPage(Map<String, Object> params) {
//        QueryWrapper<PayPieceEntity> r = new QueryWrapper<PayPieceEntity>();
//        Page<PayPieceEntity> page = this.baseMapper.selectPage(new Query<PayPieceEntity>(params).getPage(), r);
        String payId = (String) params.get("payId");
        String productionNumber = (String) params.get("productionNumber");
        String department = (String) params.get("department");
        String department2 = (String) params.get("department2");
        String name = (String) params.get("name");
        String name2 = (String) params.get("name2");
        String productCode = (String) params.get("productCode");
        String productName = (String) params.get("productName");
        String unit = (String) params.get("unit");
        String processName = (String) params.get("processName");
        String processPrice = (String) params.get("processPrice");
        String orderNum = (String) params.get("orderNum");
        String amount = (String) params.get("amount");
        String completedQuantity = (String) params.get("completedQuantity");
        String remark = (String) params.get("remark");
        String beginTime = (String) params.get("beginTime");
        String endTime = (String) params.get("endTime");
        Page<PayPieceEntity> page = payPieceMapper.findPage(
                new Query<PayPieceEntity>(params).getPage(),
                StrUtil.nullToDefault(payId, ""),
                StrUtil.nullToDefault(productionNumber, ""),
                StrUtil.nullToDefault(department, ""),
                StrUtil.nullToDefault(department2, ""),
                StrUtil.nullToDefault(name, ""),
                StrUtil.nullToDefault(name2, ""),
                StrUtil.nullToDefault(productCode, ""),
                StrUtil.nullToDefault(productName, ""),
                StrUtil.nullToDefault(unit, ""),
                StrUtil.nullToDefault(processName, ""),
                StrUtil.nullToDefault(processPrice, ""),
                StrUtil.nullToDefault(orderNum, ""),
                StrUtil.nullToDefault(amount, ""),
                StrUtil.nullToDefault(completedQuantity, ""),
                StrUtil.nullToDefault(remark, ""),
                StrUtil.nullToDefault(beginTime, ""),
                StrUtil.nullToDefault(endTime, ""),
                (String) params.get(Constant.SQL_FILTER));
        return new PageUtil(page);
    }


    @DataFilter(compAlias = "c", deptAlias = "d", userAlias = "u")
    public List<PayPieceEntity> findList(Map<String, Object> params) {
        String payId = (String) params.get("payId");
        String name = (String) params.get("name");
        String productionNumber = (String) params.get("productionNumber");
        String productCode = (String) params.get("productCode");
        String productName = (String) params.get("productName");
        String processCode = (String) params.get("processCode");
        String processName = (String) params.get("processName");
        String completedQuantity = (String) params.get("completedQuantity");
        String startTime = (String) params.get("startTime");
        String endTime = (String) params.get("endTime");
        List<PayPieceEntity> page = payPieceMapper.findList(
                new Query<PayPieceEntity>(params).getPage(),
                StrUtil.nullToDefault(payId, ""),
                StrUtil.nullToDefault(name, ""),
                StrUtil.nullToDefault(productionNumber, ""),
                StrUtil.nullToDefault(productCode, ""),
                StrUtil.nullToDefault(productName, ""),
                StrUtil.nullToDefault(processCode, ""),
                StrUtil.nullToDefault(processName, ""),
                StrUtil.nullToDefault(completedQuantity, ""),
                StrUtil.nullToDefault(startTime, ""),
                StrUtil.nullToDefault(endTime, ""),
                (String) params.get(Constant.SQL_FILTER));
        return page;
    }


    public boolean updateQuit(PayPieceEntity payPieceEntity) {
        return payPieceMapper.updateQuit(payPieceEntity);
        // return true;
    }

//    public boolean repairSave(HttpServletRequest request, Model model) throws ParseException {
//        String total_items = request.getParameter("total_items");//认领插入条数
//        int equipmentId =Integer.parseInt(request.getParameter("equipmentId"));
//        //int ids = Integer.parseInt(request.getParameter("id"));//认领插入条数
//        ValidatorUtil.validateEntity(total_items);
//        int total = Integer.valueOf(total_items);
//        List<BusRepairEntity> recordList = new ArrayList<BusRepairEntity>();//更新数据放这里面
//        List<BusRepairEntity> recordList1 = new ArrayList<BusRepairEntity>();//新增数据放这里
//        BusRepairEntity user = new BusRepairEntity();
//        BusEquipmentEntity busEquipmentEntity = busEquipmentMapper.equipmentInfo(equipmentId);
//
//        for (int i = 1; i <= total; i++) {
//            String id = "id_" + i;
//            String sendTime = "sendTime_" + i;
//            String repairTime = "repairTime_" + i;
//            String repairMoney = "repairMoney_" + i;
//            String repairStatus = "repairStatus_" + i;
//            String faultReason = "faultReason_" + i;
//            String remark = "remark_" + i;
//            Date date = new Date();//创建一个时间对象，获取到当前的时间
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
//            user.setSendTime(simpleDateFormat.parse(request.getParameter(sendTime)));
//            user.setRepairTime(simpleDateFormat.parse(request.getParameter(repairTime)));
//            user.setRepairMoney(request.getParameter(repairMoney));
//            user.setRepairStatus(request.getParameter(repairStatus));
//            user.setFaultReason(request.getParameter(faultReason));
//            user.setRemark(request.getParameter(remark));
//            user.setEquipmentId(equipmentId);
//            user.setEquipmentCode(busEquipmentEntity.getEquipmentCode());
//            user.setEquipmentName(busEquipmentEntity.getEquipmentName());
//            user.setEquipmentModel(busEquipmentEntity.getEquipmentModel());
//            user.setMeteringUnit(busEquipmentEntity.getMeteringUnit());
//            if (request.getParameter(id) != "") {
//                user.setId(Integer.parseInt(request.getParameter(id)));
//                //先去查询是否有记录
//                BusRepairEntity info = this.repairInfo(user.getId());
//                if (info != null) {
//                    recordList.add(user);
//                } else
//                    recordList1.add(user);
//            }else
//                recordList1.add(user);
//        }
//
//        if (recordList != null && !recordList.isEmpty()) {
//            this.updateRepair(recordList);
//        }
//        if (recordList1 != null && !recordList1.isEmpty()) {
//            return this.saveRepair(recordList1) ? true : false;
//        }
//        return true;
//    }

    @Transactional(rollbackFor = Exception.class)
    boolean saveRepair(List<PayPieceEntity> recordList) {
        return payPieceMapper.saveRepair(recordList);
    }

    @Transactional(rollbackFor = Exception.class)
    boolean updateRepair(List<PayPieceEntity> recordList) {
        return payPieceMapper.updateRepair(recordList);
    }


    public PayPieceEntity repairInfo(int id) {
        return payPieceMapper.repairInfo(id);
    }


    public List<PayPieceEntity> repairList(int id) {
        return payPieceMapper.repairList(id);
    }

    public boolean deleteByIds(Long[] ids) {

        return payPieceMapper.deleteByIds(ids);

    }

    public boolean updateCheck(Long id) {
        Long userId = UserUtils.getUserInfo().getId();

        return payPieceMapper.updateCheck(id, userId);

    }




}
